var oracledb = require("oracledb");

var query =function(sql,callback){
    oracledb.autoCommit=true;
    oracledb.getConnection(
        {
            user          : "gamker",
            password      : "123456",
        },
        function (err, connection)
        {
            if (err)
            {
                console.error(err.message);
                return;
            }/*else{
                console.log("连接成功");
            }*/
            connection.setAutoCommit(true)
            connection.execute(sql,{autoCommit:true}, function (err, result)
            {
                if (err)
                {
                    console.error(err.message);
                    doRelease(connection);
                    return;
                }
                callback(result.rows.map((v)=>
                {
                    return result.metaData.reduce((p, key, i)=>
                    {
                        p[key.name] = v[i];
                        return p;
                    }, {})
                }));
                doRelease(connection);
            });
        }
    );
}






function doRelease(connection) {
    connection.close(
        function(err) {
            if (err)
                console.error(err.message);
        });
}


exports.query = query;
